//
//  SliderViewController.m
//  Slider
//

#import "SliderViewController.h"

UILabel *lblSliderValue;

@implementation SliderViewController

- (void)sliderAction:(id)sender {
	int stepAmount = 10;
	float stepValue = (abs([(UISlider *)sender value]) / stepAmount) * stepAmount;
	[sender setValue:stepValue];
	
	lblSliderValue.text = [NSString stringWithFormat:@"%d", (int)stepValue];
}

- (void)viewDidLoad {
	
	[super viewDidLoad];
	
	CGRect sliderRect = CGRectMake(20,50,280,40);
	UISlider *mySlider = [[UISlider alloc] initWithFrame:sliderRect];
	
	mySlider.minimumValue = 0;
	mySlider.maximumValue = 100;
	mySlider.continuous = YES;
	
	// 아이콘 그림
	UIImage *leftTrackImage = [[UIImage imageNamed:@"left_slider.png"]
							   stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
	UIImage *rightTrackImage = [[UIImage imageNamed:@"right_slider.png"]
								stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
	UIImage *thumbImage = [UIImage imageNamed:@"apple_thumb.png"];
	UIImage *minSliderImage = [UIImage imageNamed:@"apple_min.png"];
	UIImage *maxSliderImage = [UIImage imageNamed:@"apple_max.png"];
	
	[mySlider setThumbImage:thumbImage forState:UIControlStateNormal];
	[mySlider setMinimumTrackImage:leftTrackImage forState:UIControlStateNormal];
	[mySlider setMaximumTrackImage:rightTrackImage forState:UIControlStateNormal];
	[mySlider setMinimumValueImage:minSliderImage];
	[mySlider setMaximumValueImage:maxSliderImage];
	[mySlider setValue:50.0f];
	
	// 값이 변경되는 이벤트 처리
	[mySlider addTarget:self action:@selector(sliderAction:)
	   forControlEvents:UIControlEventValueChanged];
	
	// 현재 선택된 값을 표시할 레이블
	CGRect lblRect = CGRectMake(145,100,100,20);
	lblSliderValue = [[UILabel alloc] initWithFrame:lblRect];
	lblSliderValue.backgroundColor = [UIColor clearColor];
	
	lblSliderValue.text = [NSString stringWithFormat:@"%d", (int)mySlider.value];
	
	// 슬라이더를 본문 뷰에 추가
	[self.view addSubview:mySlider];
	[self.view addSubview:lblSliderValue];
	
	[lblSliderValue release];
	[mySlider release];
}


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
